package com.wdciti.sccp.iot.springvertx.listener;

import com.wdciti.sccp.iot.common.aio.AioEngine;
import com.wdciti.sccp.iot.common.aio.DeployingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;

/**
 * @author by early
 * @blame IOT Team
 * @date 2021/10/11.
 */
@Slf4j
public class SpringWebServerInitializedListener implements ApplicationListener<WebServerInitializedEvent> {
    @Override
    public void onApplicationEvent(@NonNull WebServerInitializedEvent webServerInitializedEvent) {
        try {
            ApplicationContext context = webServerInitializedEvent.getApplicationContext();
            AioEngine manager = context.getBean(AioEngine.class);
            if (!manager.isRunning()) {
                manager.doDeploying();
            }
        } catch (BeansException e1) {
            log.error("!!IMPORTANT!!  No Found NioManager Bean,please check properties! ");
        } catch (DeployingException e2) {
            log.error("!!IMPORTANT!! Deploy result not all success!", e2);
        } catch (Exception e3) {
            log.error(e3.getMessage());
        }
    }
}
